package com.ksl.kinglockguard.domain;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ksl.kinglockguard.data.Constants;

public class MainList implements Serializable {
	public static final int TYPE_COMMON = 2;
	public static final int TYPE_OPEN_APP = 5;
	
	
	private int newsCount;
	private List<MainNews> newslist = new ArrayList<MainNews>();

	/**
     * 
     * 计算列表中的最小id，剔除cat=2的类型
     * 
     */
	public int getMinID() {
		int i = getNewsCount() - 1;
		while(newslist.get(i).getCat() == Constants.ARTICLE_CAT_AD && i >= 0)
			i--;
		if(newslist.get(i).getCat() != Constants.ARTICLE_CAT_AD)
			return newslist.get(i).getId();
		else
			return -1; 
	}

	/**
     * 
     * 计算列表中的最大id，剔除cat=2的类型
     * 
     */
	public int getMaxID() {
		int maxId = 0;
		int i = 0;
		while((newslist.get(i).getCat() == Constants.ARTICLE_CAT_AD) && (i < newslist.size()))//找到第一个类型不是2（广告）的数据项
			i++;
		if(newslist.get(i).getCat() != Constants.ARTICLE_CAT_AD)
			maxId = newslist.get(i).getId();
		else if(i >= newslist.size())
			return -1;
		for (i = i+1; i < newslist.size(); i++) {
			int tmpId = newslist.get(i).getId();
			if ((tmpId > maxId) && (newslist.get(i).getCat()!=Constants.ARTICLE_CAT_AD)) {
				maxId = tmpId;
			}
		}
		return maxId;
	}

	public int getNewsCount() {
		return newsCount;
	}

	public void setNewsCount(int newsCount) {
		this.newsCount = newsCount;
	}

	public List<MainNews> getNewslist() {
		return newslist;
	}

	public static MainList parse(JSONArray dataList) throws IOException,
			JSONException {
		MainList mlist = new MainList();
		if (null != dataList) {
			for (int i = 0; i < dataList.length(); i++) {
				JSONObject newsJson = dataList.getJSONObject(i);
				if (isDataValidate(newsJson)) {
					continue;
				}
				MainNews news = new MainNews();
				news.setId(newsJson.getInt("id"));
				news.setTitle(newsJson.getString("title"));
				news.setSummary(newsJson.getString("summary"));
				news.setArticleUrl(newsJson.getString("url"));
				news.setUpdateTime(newsJson.getString("update_time"));
				if( !newsJson.isNull("cat") ) {
					news.setCat(newsJson.getInt("cat"));
				}
				
				if(!newsJson.isNull("list_show")){
					news.setList_show(newsJson.getInt("list_show"));
				}
				
				if(!newsJson.isNull("lock_show")){
					news.setLock_show(newsJson.getInt("lock_show"));
				}
				
				if (!newsJson.isNull("slide2browser")) {
					news.setSlide2browser(newsJson.getInt("slide2browser"));
				}
				
				if(TYPE_OPEN_APP == news.getSlide2browser()){
					if (!newsJson.isNull("slide2browser_meta")) {
						JSONObject meta = newsJson
								.getJSONObject("slide2browser_meta");
						if (!meta.isNull("name")) {
							news.setPname(meta.optString("name"));
						}
						if (!meta.isNull("url")) {
							news.setAppUrl(meta.optString("url"));
						}
					}
				}else if(TYPE_COMMON == news.getSlide2browser()){
					
					if (!newsJson.isNull("slide2browser_meta")) {
						JSONObject meta = newsJson
								.getJSONObject("slide2browser_meta");
						if (!meta.isNull("id")) {
							news.setGameId(meta.getInt("id"));
						}
						if (!meta.isNull("date")) {
							news.setCateDate(meta.getString("date"));
						}
					}
				}
				
				if(!newsJson.isNull("dl_link")){
					news.setDownLoadLink(newsJson.getString("dl_link"));
				}
				
				if(!newsJson.isNull("from_url")){
					news.setFromUrl(newsJson.getString("from_url"));
				}
				
				if (!newsJson.isNull("small_img_url")) {
//					news.setImageUrl(newsJson.getString("small_img_url"));
					news.setSmallImageUrl(newsJson.getString("small_img_url"));
				}
				
				if (!newsJson.isNull("middle_img_url")) {
//					news.setMiddleimageUrl(newsJson.getString("middle_img_url"));
					news.setMiddleImageUrl(newsJson.getString("middle_img_url"));
				} 
				if (!newsJson.isNull("large_img_url")) {
					news.setLargeImageUrl(newsJson.getString("large_img_url"));
				}
				
				if (!newsJson.isNull("middle_img_width")) {
					news.setWidth(newsJson.getInt("middle_img_width"));
				} 
				if (!newsJson.isNull("middle_img_height")) {
					news.setHeight(newsJson.getInt("middle_img_height"));
				} 
				if (!newsJson.isNull("show_small_image")) {
					news.setShowSmallImage(newsJson.getString("show_small_image"));
				} 
				
				//以下20150107增加
				if(!newsJson.isNull("game_engine")){
					news.setEngineType(newsJson.getInt("game_engine"));
				}
				
				if(!newsJson.isNull("game_cp")){
					news.setCpToken(newsJson.getString("game_cp"));
				}
				
				if(!newsJson.isNull("game_flag")){
					news.setGameFlag(newsJson.getInt("game_flag"));
				}
				
				if(!newsJson.isNull("game_players")){
					news.setPlayersNumber(newsJson.getInt("game_players"));
				}
				
				
				mlist.newslist.add(news);
			}
			mlist.newsCount = mlist.newslist.size();
		}
		return mlist;
	}

	private static boolean isDataValidate(JSONObject data) {
		return data.isNull("id") || data.isNull("title") || data.isNull("url")
				|| data.isNull("slide2browser") || data.isNull("small_img_url")
				|| data.isNull("middle_img_url")
				|| data.isNull("large_img_url")
				|| data.isNull("middle_img_width")
				|| data.isNull("middle_img_height")
				|| data.isNull("show_small_image");
	}
}
